#include <iostream>
struct ListNode
{
    int val;
    ListNode *next;
    ListNode()
    {
        val = 0;
        next = nullptr;
    }
    ListNode(int x)
    {
        val = x;
        next = nullptr;
    }
    ListNode(int x, ListNode *n)
    {
        val = x;
        next = n;
    }
};
ListNode *CreateLinkedList()
{
    ListNode *n1 = new ListNode(1);
    ListNode *n2 = new ListNode(2);
    ListNode *n3 = new ListNode(3);
    ListNode *n4 = new ListNode(4);
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    return n1;
}
ListNode *reverseList(ListNode *head)
{
    ListNode *prev = nullptr;
    ListNode *current = head;
    ListNode *next = nullptr;

    while (current != nullptr)
    {
        next = current->next; // 保存当前节点指向的下一个节点

        current->next = prev; // 将当前节点的指针指向前一个节点
        prev = current;       // 将前一个节点移动到当前节点位置 迭代

        current = next; // 将当前节点移动到下一个节点位置
    }
    return prev;
}

int main()
{
    ListNode *temp = reverseList(CreateLinkedList());
    while (temp != nullptr)
    {
        std::cout << temp->val << "->";
        temp = temp->next;
    }
    std::cout << "nullptr" << std::endl;

    return 0;
}